//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		      _/_/_/_/.
// Copyright ©2013 - Sol-tribe.	_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEngine;
using UnityEditor;
using System.Collections;
using ViNoToolkit;

public class DrawScenarioTab  {

	static public void CreateANewScenario( TextAsset script = null ){
		ScenarioNode[] scenariosInScene = GameObject.FindObjectsOfType( typeof(ScenarioNode) ) as ScenarioNode[];
		for(int i=0;i<scenariosInScene.Length;i++){
			scenariosInScene[ i ].gameObject.SetActive( false );			
		}

		if( ! System.IO.Directory.Exists( "Assets/" + ViNoToolbar.m_ScenarioName ) ){
			AssetDatabase.CreateFolder( "Assets" , ViNoToolbar.m_ScenarioName );
		}
			
		GameObject scenarioObj = ViNoToolUtil.CreateANewScenario( ViNoToolbar.m_ScenarioName , ViNoToolbar.m_StartAndPlay , script );
		ViNoToolbar.m_CurrScenarioNode = scenarioObj.GetComponent<ScenarioNode>();
		ScenarioNode snode = ViNoToolbar.m_CurrScenarioNode;
		if( snode  == null ){
			Debug.Log ( "CurrScenario NULL" );
		}

		if (script == null) {
			GameObject startObj = scenarioObj.transform.FindChild ("START").gameObject;
			snode.startNode = startObj.GetComponent<ViNode>();

			EditorGUIUtility.PingObject (startObj);
	
			Selection.activeObject = startObj;		
			ViNoToolbar.m_AddedObject = startObj;			
			ViNoToolbar.m_Selected = 1;		// Jump to Nodes Tab.		
		}
	
		string scenarioFlagDataPath = "Assets/" + ViNoToolbar.m_ScenarioName + "/" + ViNoToolbar.m_ScenarioName + "Flags.asset";
//		string scenarioSaveDataPath = "Assets/" + ViNoToolbar.m_ScenarioName + "/" + ViNoToolbar.m_ScenarioName + "SaveData.asset";	
		FlagTable flagTable = ScriptableObjectUtility.CreateScriptableObject( "FlagTable" , scenarioFlagDataPath ) as FlagTable;
//		ScriptableObjectUtility.CreateScriptableObject( "ViNoSaveInfo" , scenarioSaveDataPath );				
		
		snode.flagTable = flagTable;		
		snode.m_PlayAtStart = ViNoToolbar.m_StartAndPlay;
	}

	/// <summary>
	/// Draw the specified toolBox.
	/// </summary>
	public void Draw( ViNoToolbar toolBar ){							
		ViNoToolbar.m_StartAndPlay = EditorGUILayout.Toggle( "Start And Play" , ViNoToolbar.m_StartAndPlay );
				//				EditorGUILayout.HelpBox( "
				//		toolBar.scenarioScript = EditorGUILayout.ObjectField( "Scenario Script" , toolBar.scenarioScript , typeof(TextAsset) , false ) as TextAsset;
				 		
		EditorGUILayout.BeginHorizontal();
		
			ViNoToolbar.m_ScenarioName = EditorGUILayout.TextField( "Name" , ViNoToolbar.m_ScenarioName , GUILayout.Width ( 300f ) );		
			
			// Create a new Scenario.
			if( GUILayout.Button( "New" , GUILayout.Width(100f) ) ){//, GUILayout.Height( 25f ) ) ){	
				CreateANewScenario (null);//toolBar.scenarioScript );		

				toolBar.Repaint();		
			}

/*
			if( GUILayout.Button( "Play" , GUILayout.Width(100f) ) ){//, GUILayout.Height( 25f ) ) ){	
				GameObject scenarioObj = ViNoToolUtil.CreateANewScenario( ViNoToolbar.m_ScenarioName , ViNoToolbar.m_StartAndPlay );			
				ScenarioNode node = scenarioObj.GetComponent<ScenarioNode>();
				node.useScenarioScript = true;
				node.scenarioScript = Resources.Load( ViNoToolbar.m_ScenarioName , typeof(TextAsset) ) as TextAsset;
				
			}
//*/
		EditorGUILayout.EndHorizontal();
	}
	
}
